﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using AgNet.Models;

namespace AgNet.Tests
{
    [TestClass]
    public class UserLoginTests
    {
        private static AgNetClient client;
        private static AgSession session;

        [ClassInitialize]
        public static void InitializeClient(TestContext context)
        {
            client = new AgNetClient(TestVariables.ClientId, TestVariables.AppSecret);
        }

        [TestMethod]
        public void CanLogin()
        {
            try
            {
                session = client.Login(TestVariables.Email, TestVariables.Password);

                Assert.IsNotNull(session);
                Assert.IsNotNull(session.AccessToken);
                Assert.IsFalse(session.ExpiresIn == 0);
                Assert.IsNotNull(session.RefreshToken);
            }
            catch (System.Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(AgNet.Exceptions.AgException));                
            }
        }

        [TestMethod]
        public void CanFetchUserInfo()
        {
            session = client.Login(TestVariables.Email, TestVariables.Password);
            var userInfo = client.FetchUserInfo(session);

            Assert.IsNotNull(userInfo);
            Assert.IsFalse(userInfo.HelperStatus == Models.HelperStatuses.invalid);

            if (userInfo.HelperStatus != Models.HelperStatuses.offline)
            {
                Assert.IsNotNull(userInfo.Etag);
                Assert.IsNotNull(userInfo.HelperVersion);
                Assert.IsNotNull(userInfo.NickName);
                Assert.IsFalse(userInfo.ScanState == Models.ScanStates.invalid);
            }
        }
    }
}
